//
//  BKEditPhotoController.m
//  bestkeep
//
//  Created by windCould on 16/11/10.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKEditPhotoController.h"
#import "BKShowImgCollectionView.h"
#import "BKMyCommon.h"
#import "BKUserFeedbackController.h"

@interface BKEditPhotoController ()<UIScrollViewDelegate,BKShowImgCollectionViewDelegate
>

@property (nonatomic,strong)UIButton * backBtn;
@property (nonatomic,strong)UIView * cusNavBar;
@property (nonatomic,strong)UIButton * delBtn;

@property (nonatomic,strong)UIView  * cusTabTool;
@property (nonatomic,strong)UILabel * photoNum;
@property (nonatomic,strong)UILabel * editLabel;

@property (nonatomic,strong)BKShowImgCollectionView * showImgCollectionView;

@end


@implementation BKEditPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarHidden = YES;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.showImgCollectionView.collectionView setContentOffset:CGPointMake(SCREENWIDTH * self.curIndex, 0) animated:YES];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setUpViews];
    }
    return self;
}

-(void)setCurIndex:(NSInteger)curIndex
{
    _curIndex = curIndex;
    _photoNum.text = [NSString stringWithFormat:@"%zd/%lu" ,_curIndex+1,(unsigned long)self.imgShowArr.count];
}

-(void)setImgShowArr:(NSMutableArray *)imgShowArr
{
    _imgShowArr = imgShowArr;
    
    if (_imgShowArr.count == 0) {
        
       [self backBtnClick:nil];
    }
    _showImgCollectionView.imgArr = imgShowArr;
}

#pragma mark ==================Function==================================

-(void)backBtnClick:(UIButton *)btn
{
    [UIApplication sharedApplication].statusBarHidden = NO;
    BKUserFeedbackController * feedBackVc = (BKUserFeedbackController *)self.superController;
    feedBackVc.fianlImgArr = self.imgShowArr;
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)deleteSelectedPhoto:(UIButton *)btn
{
    
    NSInteger count = self.imgShowArr.count - 1;
    
    [_imgShowArr  removeObjectAtIndex:_curIndex];
    self.imgShowArr = _imgShowArr;
    if (self.deleteBlock) {
        
        _deleteBlock(_curIndex);
    }
    
    
    if ( count == _curIndex) {
        
        self.curIndex = _curIndex-1;
    }
    
    self.curIndex = _curIndex;
    
}

-(void)showImgCollectionView:(BKShowImgCollectionView *)collectionView selectIndex:(NSInteger)index
{
    self.curIndex = index;
}

-(void)showImgCollectionView:(BKShowImgCollectionView *)collectionView collectionViewContentScrowToIndex:(NSInteger)index
{
    _curIndex = index;
    _photoNum.text = [NSString stringWithFormat:@"%zd/%lu",_curIndex+1,(unsigned long)_imgShowArr.count];
}

#pragma mark =================Layout==================================

-(void)setUpViews
{
    //自定义导航栏操作
    _cusNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60)];
    _cusNavBar.backgroundColor = RGBCOLOR(34,34, 34, 1);
    _cusNavBar.alpha = 0.7;
   
    _backBtn =  [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 44, 44)];
    [_backBtn setImage:[UIImage imageNamed:@"BK_My_navi_back"] forState:UIControlStateNormal];
    [_backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_cusNavBar addSubview:_backBtn];
    
    _delBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 60, 10, 42, 42)];
    [_delBtn setTitle:@"删除" forState:UIControlStateNormal];
    _delBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    _delBtn.titleLabel.font  = [UIFont systemFontOfSize:16];
    [_delBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_delBtn addTarget:self action:@selector(deleteSelectedPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [_cusNavBar addSubview:_delBtn];
    
    //展示图片的View
    _showImgCollectionView = [[BKShowImgCollectionView alloc]initWithFrame:self.view.bounds];
    _showImgCollectionView.isDataOfImg = YES;
    _showImgCollectionView.myDelegate = self;
    _showImgCollectionView.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _showImgCollectionView.itemSize = CGSizeMake(SCREENWIDTH,ScreenHeight);

    [self.view addSubview:_showImgCollectionView];
    [self.view addSubview:_cusNavBar];
   
    //自定义tableBar
    _cusTabTool = [[UIView alloc] initWithFrame:CGRectZero];
    _cusTabTool.backgroundColor = RGBCOLOR(34, 34, 34, 1);
    _cusTabTool.alpha = 0.7;
    [self.view addSubview:_cusTabTool];
    
    _photoNum = [[UILabel alloc] init];
    _photoNum.font = [UIFont systemFontOfSize:16];
    _photoNum.textAlignment = NSTextAlignmentCenter;
    _photoNum.textColor = [UIColor whiteColor];
    _photoNum.textAlignment = NSTextAlignmentCenter;
    [_cusTabTool addSubview:_photoNum];
    
    [self layOutViews];
}

-(void)layOutViews{
    
//   // [_cusNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.mas_equalTo(self.view.mas_top).offset(0);
//        make.left.mas_equalTo(self.view.mas_left).offset(0);
//        make.right.mas_equalTo(self.view.mas_right).offset(0);
//        make.height.mas_equalTo(@(60));
//    }];
    
//    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.mas_equalTo(self.cusNavBar.mas_top).offset(10);
//        make.left.mas_offset(self.cusNavBar.mas_left).offset(10);
//        make.size.mas_equalTo(CGSizeMake(44, 44));
//    }];
//    
//    [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        
//        make.right.mas_equalTo(self.view.mas_right).offset(-42);
//        make.size.mas_equalTo(CGSizeMake(42, 42));
//    }];
    
    [_showImgCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
    
    [_cusTabTool mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(@(44));
    }];
    

    [_photoNum mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(_cusTabTool.mas_centerY).offset(0);
        make.centerX.mas_equalTo(_cusTabTool.mas_centerX).offset(-15);
        make.size.mas_equalTo(CGSizeMake(40, 24));
    }];
    
}





@end
